<?php

namespace app\common\widget;

class Form extends \think\Controller
{

    private $widgetPath;

    /**
     * 初始化
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->widgetPath = APP_PATH . 'common/widget/form/';
    }

    /**
     * 复选框资源
     * @return string
     */
    public function icheckRes()
    {
        $parse = '<link href="/public/plugins/iCheck/custom.css" rel="stylesheet">' . PHP_EOL;
        $parse .= '<script src="/public/plugins/iCheck/icheck.min.js"></script>' . PHP_EOL;
        return $parse;
    }

    /**
     * 表单验证资源
     * @return string
     */
    public function validRes()
    {
        $parse = '<link href="/public/plugins/validform/css/style.css" rel="stylesheet">' . PHP_EOL;
        $parse .= '<script src="/public/plugins/validform/js/Validform.js"></script>' . PHP_EOL;
        return $parse;
    }

    /**
     * 日期控件资源
     * @return string
     */
    public function dateRes()
    {
        $parse = '<script src="/public/plugins/laydate/laydate.js"></script>' . PHP_EOL;
        return $parse;
    }

    /**
     * 富文本资源
     * @return string
     */
    public function ueditorRes()
    {
        $parse = '<script src="/public/plugins/ueditor/ueditor.config.js"></script>' . PHP_EOL;
        $parse .= '<script src="/public/plugins/ueditor/ueditor.all.min.js"></script>' . PHP_EOL;
        return $parse;
    }

    /**
     * 代码编辑器资源
     * @return string
     */
    public function codeRes()
    {
        $parse = <<<HTML
<link href="/public/plugins/codemirror/codemirror.css" rel="stylesheet">
<script src="/public/plugins/codemirror/codemirror.js"></script>
<script src="/public/plugins/codemirror/mode/htmlmixed/htmlmixed.js"></script>
<script src="/public/plugins/codemirror/mode/xml/xml.js"></script>
<script src="/public/plugins/codemirror/mode/javascript/javascript.js"></script>
<script src="/public/plugins/codemirror/mode/css/css.js"></script>
<script src="/public/plugins/codemirror/mode/clike/clike.js"></script>
<script src="/public/plugins/codemirror/mode/php/php.js"></script>
HTML;
        return $parse;
    }

    /**
     * ztree
     * @return string
     */
    public function ztreeRes()
    {
        $parse = '<link rel="stylesheet" href="/public/plugins/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">' . PHP_EOL;
        $parse .= '<script type="text/javascript" src="/public/plugins/ztree/js/jquery.ztree.all-3.5.min.js"></script>' . PHP_EOL;
        return $parse;
    }

    /**
     * 上传控件资源
     */
    public function uploaderRes()
    {
        $parse = '<link rel="stylesheet" href="/public/plugins/webuploader/webuploader.css"/>' . PHP_EOL;
        $parse .= '<script src="/public/plugins/webuploader/webuploader.min.js"></script>' . PHP_EOL;
        return $parse;
    }

    /**
     * 代码控件
     */
    public function code($field, $value = '')
    {
        $parse = <<<HTML
        <textarea id="{$field}" name="{$field}">{$value}</textarea>
<script>
var {$field} = CodeMirror.fromTextArea(document.getElementById("{$field}"), {
            lineNumbers: true,
            matchBrackets: true,
            mode: "application/x-httpd-php",
            indentUnit: 4,
            indentWithTabs: true,
            styleActiveLine: true,
            onChange: function (n){
                $('#{$field}').val({$field}.getValue());
            },
        });
</script>
HTML;
        return $parse;
    }

    /**
     * 富文本控件
     */
    public function ueditor($field, $value = '', $scene = 'system')
    {
        $parse = <<<HTML
<script id="{$field}" name="{$field}" type="text/plain">{$value}</script>
<script>
var {$field}Editor = UE.getEditor('{$field}',{
    serverUrl: '/plus/uploader/index.html?utype={$scene}'
});
</script>
HTML;
        return $parse;
    }

    /**
     * 单图上传控件
     */
    public function singleImg($field, $value = '', $scene = 'system')
    {
        $ucField = ucfirst(preg_replace('/[^0-9a-zA-Z]+/', '', $field)); //防止字段命名不符合规范
        $this->assign('ucField', $ucField);
        $this->assign('field', $field);
        $this->assign('value', $value);
        $this->assign('scene', $scene);
        return $this->fetch($this->widgetPath . 'singleImg.html');
    }

    /**
     * 多图上传控件
     */
    public function moreImg($field, $value = '', $scene = 'system')
    {
        if (!empty($value)) {
            $imgList = explode('|', $value);
        }
        $this->assign('field', $field);
        $this->assign('value', $value);
        $this->assign('imgList', $imgList);
        $this->assign('scene', $scene);
        return $this->fetch($this->widgetPath . 'moreImg.html');
    }

    /**
     * 多图上传控件,包含标题和描述
     */
    public function moreTextImg($field, $value = '', $tips = '请输入图片标题', $scene = 'system')
    {
        $arr = [];
        if (!empty($value) && $value !== '######') {
            $list      = explode('###', $value);
            $imgList   = explode('|', $list[0]); //图片
            $titleList = explode('|', $list[1]); //标题
            $infoList  = explode('|', $list[2]); //描述
            //图片、文字、备注
            foreach ($imgList as $k => $v) {
                $arr[$k]['imgurl'] = $v;
                $arr[$k]['title']  = $titleList[$k];
                $arr[$k]['info']   = $infoList[$k];
            }
        }
        $this->assign('field', $field);
        $this->assign('value', $value);
        $this->assign('imgList', $arr);
        $this->assign('scene', $scene);
        $this->assign('tips', $tips);
        return $this->fetch($this->widgetPath . 'moreTextImg.html');
    }

    /**
     * 附件上传控件
     */
    public function singleFile($field, $value = '', $scene = 'system')
    {
        $ucField = ucfirst(preg_replace('/[^0-9a-zA-Z]+/', '', $field)); //防止字段命名不符合规范
        $this->assign('ucField', $ucField);
        $this->assign('field', $field);
        $this->assign('value', $value);
        $this->assign('scene', $scene);
        return $this->fetch($this->widgetPath . 'singleFile.html');
    }
    /**
     * 扩展属性控件
     * @param $field
     * @param $value
     * @return mixed
     */
    public function extend($field, $value='')
    {
        $this->assign('field', $field);
        $this->assign('value', $value);
        $content = $this->fetch($this->widgetPath . 'extend.html');
        return $content;
    }

    /**
     * 省市区级联
     * @param array $fields 级联字段名数组
     * @param array $value 级联值数组
     * @return mixed
     */
    public function areaName($field = 'prov_id,city_id,dist_id', $value = '0,0,0')
    {
        $this->assign('fields', explode(',', $field));
        $this->assign('value', explode(',', $value));
        return $this->fetch($this->widgetPath . 'areaName.html');
    }

}